﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Threading;
using System.IO;
using System.Xml.Serialization;
using System.Text.RegularExpressions;

using DragDraw.UserService;
using DragDraw.Util;


namespace DragDraw
{    
    public partial class LoginControl : UserControl, ISystemServiceListener
    {        
        public LoginControl()
        {
            InitializeComponent();
        }

        public void Init()
        {
            tbRealname.Visibility = Visibility.Collapsed;
            tbMsg.Text = "";
            txtRealname.Visibility = Visibility.Collapsed;
            btnClose.Visibility = Visibility.Collapsed;
            btnOK.Visibility = Visibility.Collapsed;
            btnLogin.Visibility = Visibility.Visible;
            btnRegister.Visibility = Visibility.Visible;
            txtPassword.Password = "";
            txtUsername.Text = "";
            txtRealname.Text = "";
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            //check syntax
            Regex r = new Regex("[a-zA-Z0-9]");
            if (r.IsMatch(txtUsername.Text) == false)
            {
                tbMsg.Text = "用户名不合法！";
                return;
            }
            OperationManager.Instance.Login(txtUsername.Text, txtPassword.Password);
            OperationManager.Instance.CurrentSpaceManager.ShowModalDialog(null);
        }

        private void btnRegister_Click(object sender, RoutedEventArgs e)
        {
            tbRealname.Visibility = Visibility.Visible;
            txtRealname.Visibility = Visibility.Visible;
            btnClose.Visibility = Visibility.Visible;
            btnOK.Visibility = Visibility.Visible;
            btnLogin.Visibility = Visibility.Collapsed;
            btnRegister.Visibility = Visibility.Collapsed;
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            //check syntax
            Regex r = new Regex("[a-zA-Z0-9]");
            if (r.IsMatch(txtUsername.Text) == false)
            {
                tbMsg.Text = "用户名不合法！";
                return;
            }

            UserInfo info = new UserInfo()
            {
                UserID = txtUsername.Text,
                Realname = txtRealname.Text
            };
            ServiceManager.Instance.Register(info, txtPassword.Password, this);
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Init();
        }

        public void OnLogin(LoginCompletedEventArgs e) { }
        public void OnRegister(RegisterCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                Init();
                tbMsg.Text = "注册成功，可以登陆~";
            }
            else
            {
                tbMsg.Text = "注册失败！";
                ErrorManager.Instance.ShowLastServiceError();
            }
        }
        public void OnGetSystemSetting(GetSystemSettingCompletedEventArgs e) { }
    }
}
